{% extends "reg6/reg.html" %}
{% load i18n %}
{% load reg6_filters %}
{% block content %}
<div id="content-main">
<style type="text/css">
.reg6_form_error { color: #FF0000; font-weight: bold }
.reg6_label { font-weight: normal }
b { font-weight: bold }
</style>

{% if ticket and total %}
    {% if promo %}
    <p>
    You are using promo code <b>{{ promo }}</b>.
    </p>
    {% endif %}

    <p>
    Your {{ ticket.description }} costs {{ ticket.price|money }}.
    </p>

    {% if items %}
    <p>
    You added the following additional items:
    </p>

    <p>
    {% for item in items %}
        * {{ item.description }} ({{ item.price|money }})<br />
    {% endfor %}
    </p>
    {% endif %}

    {% if offset_item %}
    <p>
    You received the following credits:
    </p>

    <p>
    * {{ offset_item.description }} waives the ticket price of
      {{ ticket.price|money }}.
    </p>
    {% endif %}

    <p>
    Your total is {{ total|money }}.
    </p>

    <p>
    We now need the following information from you. The fields in <b>bold</b>
    are required.
    </p>

    {% if pgp_questions %}
    <script language="JavaScript">
    var max_num_keys = {{ pgp_num_keys }};
    var num_keys = 1;

    function pgpcheck_impl(fingerprint, size, type, blank_ok) {
      var str = fingerprint;
      while (str.indexOf(' ') >= 0) {
        str = str.replace(' ', '');
      }
      if (blank_ok && str.length == 0 && size.length == 0 && !type)
        return true;
      if (str.length == 40) {
        for (var i = 0; i < str.length; ++i) {
          var c = str[i];
          if ((c >= '0' && c <= '9') ||
              (c >= 'a' && c <= 'f') ||
              (c >= 'A' && c <= 'F')) {
            continue;
          }
          alert('PGP fingerprint character ' + i + ' is invalid: ' + c + '.');
          return false;
        }
      } else {
        alert('PGP fingerprint should be 40 characters long. Input is ' +
              str.length + ' characters. (If yours is 32, your key is ' +
              'ancient and unsafe, please generate a new one.)');
        return false;
      }

      var valid_size = false;
      size = parseInt(size);
      if (size > 0 && size < 9999) {
        valid_size = true;
      }
      if (!valid_size) {
        alert('PGP fingerprint size is invalid');
        return false;
      }

      if (!type) {
        alert('PGP fingerprint type is invalid');
        return false;
      }
      return true;
    }

    function pgpcheck() {
      var no_want_pgp = document.getElementById('no_pgp');
      if (no_want_pgp.checked) {
        return true;
      }

      for (i = 0; i < num_keys; ++i) {
        var q_key_idx = {{ pgp_question1_index }} + i * 3;
        var q_size_idx = q_key_idx + 1;
        var q_type_idx = q_key_idx + 2;
        var q_key = document.getElementsByName('tq' + q_key_idx);
        var q_size = document.getElementsByName('tq' + q_size_idx);
        var q_type = document.getElementsByName('lq' + q_type_idx);
        if (q_key.length == 0 || q_size.length == 0 || q_type.length == 0) {
          alert('Something is wrong with the PGP check, please report.');
          return true;
        }
        var blank_ok = i > 0;
        if (!pgpcheck_impl(q_key[0].value, q_size[0].value, q_type[0].value, blank_ok)) {
          return false;
        }
      }
      return true;
    }

    function toggle_pgp() {
      var no_want_pgp = document.getElementById('no_pgp');
      var display = no_want_pgp.checked ? "none" : "inline";
      document.getElementById('pgp_section').style.display = display;
      return true;
    }

    function pgp_add_key() {
      if (num_keys >= max_num_keys) {
        alert(max_num_keys + " is the max allowed keys");
        return;
      }
      var q1_key_index = {{ pgp_question1_index }};
      var start_index = q1_key_index + (num_keys * 3);
      var end_index = start_index + 3;
      for (i = start_index; i< end_index; ++i) {
        document.getElementById("pgp_row" + i).style.display = 'block';
      }
      num_keys++;
    }
    </script>

    <form method="post" action="." onsubmit="return pgpcheck()">
    {% else %}
    <form method="post" action=".">
    {% endif %}

    <input type="hidden" name="promo" value="{{ promo }}" />
    <input type="hidden" name="ticket" value="{{ ticket.name }}" />
    {% for item in items %}
        <input type="hidden" name="item{{ forloop.counter0 }}" value="{{ item.name }}" />
    {% endfor %}

    <table>
    {% if form.salutation.errors %}
    <tr>
        <td colspan="2" class="reg6_form_error">{{ form.salutation.errors|join:", " }}</td>
    </tr>
    {% endif %}
    <tr>
        <td><label class="reg6_label" for="id_salutation">Salutation:</label></td>
        <td>{{ form.salutation }}</td>
    </tr>
    {% if form.first_name.errors %}
    <tr>
        <td colspan="2" class="reg6_form_error">{{ form.first_name.errors|join:", " }}</td>
    </tr>
    {% endif %}
    <tr>
        <td><label class="reg6_label" for="id_first_name"><b>First Name:</b></label></td>
        <td>{{ form.first_name }}</td>
    </tr>
    {% if form.last_name.errors %}
    <tr>
        <td colspan="2" class="reg6_form_error">{{ form.last_name.errors|join:", " }}</td>
    </tr>
    {% endif %}
    <tr>
        <td><label class="reg6_label" for="id_last_name"><b>Last Name:</b></label></td>
        <td>{{ form.last_name }}</td>
    </tr>
    {% if form.title.errors %}
    <tr>
        <td colspan="2" class="reg6_form_error">{{ form.title.errors|join:", " }}</td>
    </tr>
    {% endif %}
    <tr>
        <td><label class="reg6_label" for="id_title">Title:</label></td>
        <td>{{ form.title }}</td>
    </tr>
    {% if form.org.errors %}
    <tr>
        <td colspan="2" class="reg6_form_error">{{ form.org.errors|join:", " }}</td>
    </tr>
    {% endif %}
    <tr>
        <td><label class="reg6_label" for="id_org">Organization:</label></td>
        <td>{{ form.org }}</td>
    </tr>
    {% if form.zip.errors %}
    <tr>
        <td colspan="2" class="reg6_form_error">{{ form.zip.errors|join:", " }}</td>
    </tr>
    {% endif %}
    <tr>
        <td><label class="reg6_label" for="id_zip"><b>Zip/Postal Code:</b></label></td>
        <td>{{ form.zip }}</td>
    </tr>
    {% if form.email.errors %}
    <tr>
        <td colspan="2" class="reg6_form_error">{{ form.email.errors|join:", " }}</td>
    </tr>
    {% endif %}
    <tr>
        <td><label class="reg6_label" for="id_email"><b>Email:</b></label></td>
        <td>{{ form.email }}</td>
    </tr>
    {% if form.phone.errors %}
    <tr>
        <td colspan="2" class="reg6_form_error">{{ form.phone.errors|join:", " }}</td>
    </tr>
    {% endif %}
    <tr>
        <td><label class="reg6_label" for="id_phone">Phone:</label></td>
        <td>{{ form.phone }}</td>
    </tr>
    {% if form.can_email.errors %}
    <tr>
        <td colspan="2" class="reg6_form_error">{{ form.can_email.errors|join:", " }}</td>
    </tr>
    {% endif %}
    <tr>
        <td><label class="reg6_label" for="id_can_email">Add me to the announcement mailing list:</label></td>
        <td>{{ form.can_email }}</td>
    </tr>
    </table>

    {% if questions %}
    <p>
    Survey Questions:
    </p>

    {% if form.answers.errors %}
    <p>
    <span class="reg6_form_error">{{ form.answers.errors|join:", " }}</span>
    </p>
    {% endif %}

    <p>
    <div>
    {% for q in questions %}
        <label>
        <div>{{ q.text }}</div>
        <div>
        {% ifequal q.type_value 'list' %}
            <select name="lq{{ q.id }}" size="1">
                <option value="" selected="selected"></option>
                {% for ans in q.get_answers %}
                <option value="{{ ans.id }}">{{ ans.text }}</option>
                {% endfor %}
            </select>
        {% else %}
            <input type="text" name="tq{{ q.id }}"
                   maxlength="{{ q.max_length }}" />
        {% endifequal %}
        </div>
        </label>
    {% endfor %}
    </div>
    </p>
    {% endif %}

    {% if pgp_questions %}

    <p>
    Key Signing Party Questions:
    </p>

    <p>
    <label>
        Wait - I don't know what PGP is! Nevermind! Ignore the PGP stuff!
        <input type="checkbox" name="no_pgp" id="no_pgp" onclick="toggle_pgp()" value="true" />
    </label>

    <div id='pgp_section'>
    <p>
    <b>All keys must be on either pgp.mit.edu or pool.sks-keyservers.net.</b>
    </p>

    <div>
    {% for q in pgp_questions %}
        <label>
        {% if q.id < pgp_question2_index %}
        <div id="pgp_row{{ q.id }}">
        {% else %}
        <div id="pgp_row{{ q.id }}" style="display: none;">
        {% endif %}
            <div>
            {% if q.id < pgp_question2_index %}
                <b>{{ q.text }}</b>
            {% else %}
                {{ q.text }}
            {% endif %}
            </div>
            <div>
            {% ifequal q.type_value 'list' %}
                <select name="lq{{ q.id }}" size="1">
                    <option value="" selected="selected"></option>
                    {% for ans in q.get_answers %}
                    <option value="{{ ans.id }}">{{ ans.text }}</option>
                    {% endfor %}
                </select>
            {% else %}
                <input type="text" name="tq{{ q.id }}" maxlength="{{ q.max_length }}" />
            {% endifequal %}
            </div>
        </div>
        </label>
    {% endfor %}
    </div>

    <p>
    <button type="button" onclick="pgp_add_key()">I want to add more keys</button>
    </p>

    </div>
    {% endif %}

    <p>
    <input type="submit" value="Continue" />
    </p>
    </form>

    <form method="post" action="../">
    <p>
    <input type="hidden" name="promo" value="{{ promo }}" />
    <input type="submit" value="Start Over" />
    </p>
    </form>
{% else %}
Registration is closed.
{% endif %}
</div>
{% endblock %}

